const { Mongoose } = require('mongoose');
var mongoose = require('../common/db.js');

var userSchema = new mongoose.Schema({
    userName: String,
    password: String,
    userMail: String,
    userPhone: String,
    userAdmin: String,
    userPower: String,
    userStop: String
});


userSchema.statics.findAll = function () {
    return this.find()
}

userSchema.statics.findByUserName = function (name) {
    return this.find({ userName: name }).exec();
}

userSchema.statics.findUserLogin = function (name, password) {
    return this.find({ userName: name, password: password, userStop: 0 });
}

userSchema.statics.findUserPassword = function (name, mail, phone, callBack) {
    this.find({ userName: name, userMail: mail, userPhone: phone }, callBack);
};

userSchema.statics.updateUserPassword = function (userId, newPassword) {
    return this.updateOne({ _id: new mongoose.Types.ObjectId(userId) }, { $set: { 'password': newPassword } })
    .then(result => {
        console.log(result);
    })
}

var userModel = mongoose.model('user', userSchema);
module.exports = userModel;
